package com.darwin.darwinUtils
{
	import flash.net.SharedObject;

	/**
	 * 本地缓存工具类 
	 * 游戏中所有对于本地缓存的操作均需要通过该类操作 
	 * @author vanCopper
	 * 
	 */	
	public class SharedObjectUtil
	{
		private static var sharedObject:SharedObject;
		public function SharedObjectUtil()
		{
			try
			{
				FM_SHARE_OBJ = SharedObject.getLocal(FM_DIR)
			}catch(e:Error)
			{
				
			}
		}
		
		/**
		 * 创建一个sharedObject 
		 * @param name
		 * @return 
		 * 
		 */		
		public static function creatSharedObject(name:String):SharedObject
		{
			var sharedObj:SharedObject;
			try
			{
				sharedObj = SharedObject.getLocal(name,"/");
			}catch(e:Error)
			{
				
			}
			return sharedObj;
		}
		
		/**
		 * 获得本地sharedObject数据 
		 * @param name
		 * @return 
		 * 
		 */		
		public static function getSharedObjectData(name:String):Object
		{
			try
			{
				sharedObject = SharedObject.getLocal(name,"/");
			}catch(e:Error)
			{
				return null;
			}
			return sharedObject.data;
		}
		
		/**
		 * 为本地存储对象添加数据 
		 * @param name 
		 * @param key
		 * @param data
		 * 
		 */		
		public static function writeSharedObjectData(name:String,key:String,data:*):void
		{
			try
			{
				sharedObject = SharedObject.getLocal(name,"/");
				sharedObject.data[key] = data;
				sharedObject.flush(1000);
			}catch(e:Error)
			{
				return;
			}
		}
		
		/**
		 * 读取本机存储对象数据  
		 * @param name
		 * @param key
		 * @return 
		 * 
		 */		
		public static function readSharedObjectData(name:String,key:String):Object
		{
			try
			{
				sharedObject = SharedObject.getLocal(name,"/");
				return sharedObject.data[key];
			}catch(e:Error)
			{
				return null;
			}
			return null;
		}
		
		////-------加载校验用-------/////
		private static const FM_DIR:String = "fmDir";
		
		public static function fmWriteSharedObject(key:String,url:String):void
		{
			try
			{
				var sharedObj:SharedObject = SharedObject.getLocal(FM_DIR);
				sharedObj.data[key] = url;
				sharedObj.flush(1000);
			}catch(e:Error)
			{
				
			}
		}
		
		private static var FM_SHARE_OBJ:SharedObject; 
		public static function fmReadSharedObject(key:String):String
		{
			try
			{
				if(FM_SHARE_OBJ == null)
				{
					FM_SHARE_OBJ = SharedObject.getLocal(FM_DIR,"/");
				}
			}catch(e:Error)
			{
				
			}
			
			var data:Object = FM_SHARE_OBJ.data;
			var returnStr:String = "";
			for(var i:String in data)
			{
				if(i == key)
				{
					returnStr = data[key];
				}
			}
			return returnStr;
		}
		
	}
}